package com.handong.handserialprogram.utils;

import com.google.gson.Gson;
import com.handong.handserialprogram.model.bean.TagCoordinateModel;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

/**
 * Created by Administrator on 2016/12/7.
 */

public class ObjectToByteArray {

    public static byte[] toByteArray(Object obj) {
        ByteArrayOutputStream out = null;
        ObjectOutputStream objectOut = null;
        byte[] result = null;
        try {
            out = new ByteArrayOutputStream();
            objectOut = new ObjectOutputStream(out);
            objectOut.writeObject(obj);
            result = out.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }

    public static byte[] addArrayLength(byte[] buff, int len) {
        byte[] result = new byte[len];
        int length = buff.length;
        System.arraycopy(buff, 0, result, len - length, length);
        return result;
    }

    public static byte[] intToByteArray(int res) {
        byte[] targets = new byte[4];

        targets[0] = (byte) (res & 0xff);// 最低位
        targets[1] = (byte) ((res >> 8) & 0xff);// 次低位
        targets[2] = (byte) ((res >> 16) & 0xff);// 次高位
        targets[3] = (byte) (res >>> 24);// 最高位,无符号右移。
        return targets;
    }

    public static byte[] shortToByteArray(short res) {
        byte[] targets = new byte[2];
        targets[0] = (byte) (res & 0xff);// 最低位
        targets[1] = (byte) ((res >>> 8) & 0xff);// 高位
        return targets;
    }
    /**
     * 上传数据
     * @param coordinateModel
     * @param cmd
     * @return
     */
    public static byte[] getUploadByte(TagCoordinateModel coordinateModel, byte cmd)
    {
        Gson gson = new Gson();
        byte[] buff = gson.toJson(coordinateModel).getBytes();
        int i = buff.length;
        byte[] resultArr = new byte[i + 3];
        resultArr[0] = 50;
        resultArr[(resultArr.length - 1)] = 50;
        resultArr[1] = cmd;
        System.arraycopy(buff, 0, resultArr, 2, i);
        return resultArr;
    }

    /**
     * 上传数据
     * @param content
     * @param cmd
     * @return
     */
    public static byte[] getCloseAlarmByteArr(String content, byte cmd) {
        byte[] buff = content.getBytes();
        int i = buff.length;
        byte[] resultArr = new byte[i + 3];
        resultArr[0] = 50;
        resultArr[(resultArr.length - 1)] = 50;
        resultArr[1] = cmd;
        System.arraycopy(buff, 0, resultArr, 2, i);
        return resultArr;
    }

    /**
     * 根据实体类生产可上传的字节数组。
     *
     * @param model
     * @return
     */
   /* public static byte[] getUploadByte(TagCoordinateModel model, short command) {

        Gson gson = new Gson();
        byte[] data = gson.toJson(model).getBytes();
        int len = data.length;
        byte[] uploadData = new byte[len + 10];

        //得到上传数组长度。
        byte[] lengthArr = intToByteArray(len + 10);
        byte[] commandArr = shortToByteArray(command);

        //复制数组。
        System.arraycopy(lengthArr, 0, uploadData, 0, 4);
        System.arraycopy(commandArr, 0, uploadData, 4, 2);
        System.arraycopy(data, 0, uploadData, 6, len);
        System.arraycopy(lengthArr, 0, uploadData, len + 6, 4);
        return uploadData;
    }*/

    /**
     * 根据实体类生产可上传的字节数组。
     *
     * @param model
     * @return
     */
  /*  public static byte[] getUploadByte(Object model, short command) {

        Gson gson = new Gson();
        byte[] data = gson.toJson(model).getBytes();
        int len = data.length;
        byte[] uploadData = new byte[len + 10];

        //得到上传数组长度。
        byte[] lengthArr = intToByteArray(len + 10);
        byte[] commandArr = shortToByteArray(command);

        //复制数组。
        System.arraycopy(lengthArr, 0, uploadData, 0, 4);
        System.arraycopy(commandArr, 0, uploadData, 4, 2);
        System.arraycopy(data, 0, uploadData, 6, len);
        System.arraycopy(lengthArr, 0, uploadData, len + 6, 4);
        return uploadData;
    }*/
}
